﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.XmlDataProviderCore;
using MovieRental.DataInterfaces;
using MovieRental.Entities;
using MovieRental.GeneralDataExceptions;

namespace MovieRental.XmlRepositoryLibrary
{
    public class XmlCustomerRepository : XmlRepository<CustomerInfo>, ICustomerRepository
    {
        public XmlCustomerRepository(XmlDataProvider provider)
            :base(provider)
        {

        }

        public CustomerInfo Load(Guid id)
        {
            using (var context = CreateContext())
            {
                var set = context.GetAllForType(typeof(CustomerInfo)).Select(x => (CustomerInfo)x);
                try
                {
                    return set.Single(x => x.Id == id);
                }
                catch (Exception ex)
                {
                    throw new ItemNotFoundException(id.ToString(), ex);
                }
            }
        }

        public void Delete(Guid id)
        {
            CustomerInfo customer = new CustomerInfo(id);
            Delete(customer);
        }

        public bool ContainsCustomer(Guid id)
        {
            try
            {
                Load(id);
            }
            catch (ItemNotFoundException)
            {
                return false;
            }
            return true;
        }
    }
}
